package com.classic.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.classic.adapter.ListAdapter;
import com.classic.base.LazyLoadFragment;
import com.classic.content.contents;
import com.classic.dialog.SelectLabelDialog;
import com.classic.duanzhi.R;

import java.util.Arrays;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class EpisodeListFragment extends LazyLoadFragment implements SelectLabelDialog.OnItemClickListener {

    private ViewPager viewpager;
    private SelectLabelDialog dialog;
    private int index = 0;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.ad_fragment_episode_list;
    }

    private static final String SHOW_TITLE = "SHOW_TITLE";
    private static final String SHOW_BACK = "SHOW_BAKC";
    private static final String ISSETTINGTITLECOLOR = "isSettingTitleColor";
    private static final String TITLE = "TITLE";

    /**
     * @param title true 显示title  false 不显示  默认不显示
     * @return 当前fragment
     */
    public static EpisodeListFragment getInstance(boolean showTitle, String title) {
        EpisodeListFragment fragment = new EpisodeListFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(SHOW_TITLE, showTitle);
        bundle.putString(TITLE, title);
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * @param title    true 显示title  false 不显示  默认不显示
     * @param showBack true 显示title  false 不显示  默认不显示
     * @param isSettingTitleColor true 设置透明色颜色  false 设置颜色值  默认设置
     * @return 当前fragment
     */
    public static EpisodeListFragment getInstance(boolean showTitle, String title, boolean showBack,boolean isSettingTitleColor) {
        EpisodeListFragment fragment = new EpisodeListFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(SHOW_TITLE, showTitle);
        bundle.putString(TITLE, title);
        bundle.putBoolean(SHOW_BACK, showBack);
        bundle.putBoolean(ISSETTINGTITLECOLOR, isSettingTitleColor);
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * @return 当前fragment 默认什么都不显示的情况
     */
    public static EpisodeListFragment getInstance() {
        EpisodeListFragment fragment = new EpisodeListFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(SHOW_TITLE, false);
        bundle.putString(TITLE, "");
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initView() {
        viewpager = rootView.findViewById(R.id.viewpager);
        TabLayout tablayout = rootView.findViewById(R.id.tablayout);
        Bundle arguments = getArguments();
        if (null != arguments) {
            LinearLayout layout = rootView.findViewById(R.id.mToolbarLayout);
            TextView back = rootView.findViewById(R.id.iv_title_back);
            back.setVisibility(arguments.getBoolean(SHOW_BACK) ? View.VISIBLE : View.GONE);
            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null == listener) return;
                    listener.onClick();
                }
            });
            layout.setVisibility(View.GONE);
            if (arguments.getBoolean(SHOW_TITLE)) {
                TextView appName = rootView.findViewById(R.id.tv_title_name);
                layout.setVisibility(View.VISIBLE);
                if (!arguments.getBoolean(ISSETTINGTITLECOLOR)){
                    layout.setBackgroundColor(getResources().getColor(contents.TITLE_BACKGROUND));
                }
                appName.setTextColor(getResources().getColor(contents.titleColor));
                layout.setPadding(0, getStatusBarHeight(getContext()), 0, 0);
                appName.setText(TextUtils.isEmpty(arguments.getString(TITLE)) ? getResources().getString(R.string.app_name) : arguments.getString(TITLE));
            }
        }

        final String[] strings = getResources().getStringArray(R.array.table);
        ListAdapter listAdapter = new ListAdapter(getChildFragmentManager(), Arrays.asList(strings));
        viewpager.setAdapter(listAdapter);
        tablayout.setupWithViewPager(viewpager);

        rootView.findViewById(R.id.iv_more).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initSelectDialog(Arrays.asList(strings));
            }
        });
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                index = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    void initSelectDialog(List<String> strings) {
        if (null == dialog) dialog = new SelectLabelDialog(getContext(), strings, this);
        dialog.show(index);
    }

    @Override
    protected void initData() {

    }

    public EpisodeListFragment() {
        // Required empty public constructor
    }


    @Override
    public void fetchData() {

    }

    @Override
    public void onClick(int index) {
        if (null!=viewpager)viewpager.setCurrentItem(index);

    }

    private OnClickListener listener;

    public OnClickListener getListener() {
        return listener;
    }

    public void setListener(OnClickListener listener) {
        this.listener = listener;
    }

    public interface OnClickListener {
        void onClick();
    }
}
